package com.java2007.zhangyu.restaurant.controller;

import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.service.impl.FoodTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/23
 */
@WebServlet("/foodtype/search")
public class FoodTypeSearchServlet extends HttpServlet {

    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            //查询所有菜品类别的信息
            //List<FoodType> foodTypes = foodTypeService.queryAll();
            //将查询到的菜类别放到request域中
            //request.setAttribute("foodTypes",foodTypes);

            //获取请求参数
            String fTypeName = request.getParameter("fTypeName");
            //根据菜类别名模糊查询菜类别的信息
            List<FoodType> types = foodTypeService.queryByFoodTypeName(fTypeName);
            //将查询到的信息放入request域中
            request.setAttribute("types",types);
            request.setAttribute("fTypeName",fTypeName);
            //转发
            request.getRequestDispatcher("/backend/detail/foodtype/food-type-list.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙，请稍后重试！");
            request.getRequestDispatcher("../../backend/index.jsp").forward(request, response);
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
